package cn.edu.ncu.housinghub.mapper;

import cn.edu.ncu.housinghub.entity.user.Agent;
import cn.edu.ncu.housinghub.entity.user.IdentityInfo;
import cn.edu.ncu.housinghub.entity.user.Landlord;
import cn.edu.ncu.housinghub.entity.user.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select userId id,username,password,email,role,isIdentityVerified from user")
    //  查询所有用户
    List<User> selectAll();

    //  根据邮箱查询用户
    @Select("select userId id,username,password,email,phone,gender,birthday,city,bio,avatar,role,createTime,isIdentityVerified from user " +
            "where email = #{email}")
    User selectByEmail(String email);

    //  添加用户
    @Insert(" insert into user(username, email, password, role,createTime,isIdentityVerified) " +
            "values(#{username}, #{email}, #{password}, #{role},#{createTime},#{isIdentityVerified})")
    int insertUser(User user);

    // 根据id查找用户
    @Select("select userId id,username,password,email,phone,gender,birthday,city,bio,avatar,role,createTime,isIdentityVerified from user where userId=#{id}")
    User selectById(Long id);

    // 修改用户
    @Update("<script>" +
            "update `user` " +
            "<set>" +
            "<if test='username != null'>username = #{username},</if>" +
            "<if test='email != null'>email = #{email},</if>" +
            "<if test='password != null'>password = #{password},</if>" +
            "<if test='phone != null'>phone = #{phone},</if>" +
            "<if test='gender != null'>gender = #{gender},</if>" +
            "<if test='birthday != null'>birthday = #{birthday},</if>" +
            "<if test='city != null'>city = #{city},</if>" +
            "<if test='bio != null'>bio = #{bio},</if>" +
            "<if test='avatar != null'>avatar = #{avatar},</if>" +
            "<if test='role != null'>role = #{role},</if>" +
            "</set>" +
            "where userId = #{id}" +
            "</script>")
    void updateUser(User user);

    // 更新用户个人资料
    @Update("<script>" +
            "update `user` " +
            "<set>" +
            "<if test='username != null'>username = #{username},</if>" +
            "<if test='phone != null'>phone = #{phone},</if>" +
            "<if test='gender != null'>gender = #{gender},</if>" +
            "<if test='birthday != null'>birthday = #{birthday},</if>" +
            "<if test='city != null'>city = #{city},</if>" +
            "<if test='bio != null'>bio = #{bio},</if>" +
            "<if test='avatar != null'>avatar = #{avatar},</if>" +
            "</set>" +
            "where userId = #{id}" +
            "</script>")
    void updateUserProfile(User user);

    // 更新用户密码
    @Update("update `user` set password = #{password} where userId = #{id}")
    void updatePassword(User user);
    // 添加用户认证信息
    @Insert("insert into identityinfo(userId,realName,idCard) values(#{userId},#{realName},#{idCard})")
    void insertIdentityInfo(IdentityInfo identityInfo);

    // 根据用户ID查询实名认证信息
    @Select("select userId,realName,idCard,faceVerify from identityinfo where userId=#{userId}")
    IdentityInfo selectIdentityInfoByUserId(Long userId);

    // 根据id查找用户认证信息
    @Select("select isIdentityVerified from user where userId=#{id}")
    Boolean selectIdentityVerifiedById(Integer id);

    // 修改用户实名认证状态
    @Update("update user set isIdentityVerified = #{isIdentityVerified} where userId= #{id}")
    void updateIdentityVerified(User user);

    // 修改用户角色为房东
    @Update("update user set role = 2 where userId = #{userId}") // 2=房东角色
    void updateRoleToLandlord(Long userId);

    // 修改用户角色为中介
    @Update("update user set role = 4 where userId = #{userId}")
    void updateRoleToAgent(Long userId);

    // 新增中介记录
    @Insert("insert into agent(userId,agentVerifyId,licenseNo,licensePicPath,phone,companyName," +
            "companyPosition,realName,idCard,email,city,workYears) " +
            "values(#{userId},#{agentVerifyId},#{licenseNo},#{licensePicPath},#{phone},#{companyName}," +
            "#{companyPosition},#{realName},#{idCard},#{email},#{city},#{workYears})")
    void addNewAgent(Agent agent);

    // 新增房东记录（用户转为房东时调用）
    @Insert("insert into landlord(userId, isDeleted) " +
            "values(#{userId}, 0)") // 0=未删除
    void insertNewLandlord(Landlord landlord);

    // 通过用户ID查询房东信息
    @Select("select * from landlord where userId = #{userId}")
    Landlord selectLandlordByUserId(Long userId);

    @Select("select * from landlord where landlordId = #{landlord}")
    Landlord selectLandlordByLandlordId(Long landlord);

    /**
     * 通过用户ID查询中介信息
     * @param userId
     * @return
     */
    @Select("select * from agent where userId = #{userId} ")
    Agent selectAgentByUserId(Long userId);
}
